<?php

function ipActivityNormalCommentAdding($db, $remoteAdrress, $limit) {
    $limit--;
    $db->dbq("
                Select
                    Count(*)
                From
                    ipactivitys
                Where
                    ip = \"" . $remoteAdrress . "\"
            ");

    $arr = $db->row();
    if ($arr[0] == "0") {
        $activity = array();
        $activity["comment"] = array();
        $activity["comment"][] = time();
        $activity = mysql_real_escape_string(json_encode($activity));
        $db->dbq("
                    Insert Into
                        ipactivitys
                    Values
                        (
                            0,
                            \"" . $remoteAdrress . "\",
                            \"" . $activity . "\"
                        )
                ");

        if ($limit < 2) {
            return 0.5;
        }

        return 1;
    } else {
        $db->dbq("
                    Select
                        activity
                    From
                        ipactivitys
                    Where
                        ip = \"" . $remoteAdrress . "\"
                ");

        $arr = $db->row();
        $activity = json_decode($arr[0], true);
        if (count($activity["comment"]) == 0) {
            $activity["comment"] = array();
            $activity["comment"][] = time();
            $activity = mysql_real_escape_string(json_encode($activity));
            $db->dbq("
                        Update
                            ipactivitys
                        Set
                            activity = \"" . $activity . "\"
                        Where
                            ip = \"" . $remoteAdrress . "\"
                    ");

            if ($limit < 2) {
                return 0.5;
            }

            return 1;
        } else {
            $count = 0;
            foreach ($activity["comment"] as $index => $timeStamp) {
                if ((time() - $timeStamp) < 3600) {
                    $count++;
                }
            }

            if ($count > $limit) {
                return 0;
            } else {
                $activity["comment"][] = time();
                $activity = mysql_real_escape_string(json_encode($activity));
                $db->dbq("
                            Update
                                ipactivitys
                            Set
                                activity = \"" . $activity . "\"
                            Where
                                ip = \"" . $remoteAdrress . "\"
                        ");

                if ($count == ($limit - 1)) {
                    return 0.5;
                }

                return 1;
            }
        }
    }
}

function ipActivityNormalComment($db, $remoteAdrress, $limit) {
    $limit--;
    $db->dbq("
                Select
                    Count(*)
                From
                    ipactivitys
                Where
                    ip = \"" . $remoteAdrress . "\"
            ");

    $arr = $db->row();
    if ($arr[0] == "0") {
        return 1;
    } else {
        $db->dbq("
                    Select
                        activity
                    From
                        ipactivitys
                    Where
                        ip = \"" . $remoteAdrress . "\"
                ");

        $arr = $db->row();
        $activity = json_decode($arr[0], true);
        if (count($activity["comment"]) == 0) {
            return 1;
        } else {
            $count = 0;
            foreach ($activity["comment"] as $index => $timeStamp) {
                if ((time() - $timeStamp) < 3600) {
                    $count++;
                }
            }

            if ($count > $limit) {
                return 0;
            } else {
                if ($count == ($limit - 1)) {
                    return 0.5;
                }

                return 1;
            }
        }
    }
}
?>